package com.haut.servlet;

import com.haut.bean.Staff;
import com.haut.service.AdminService;
import com.haut.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet({"/admin/login","/admin/back","/admin/update","/admin/select"})
public class AdminServlet extends HttpServlet{
    AdminService adminService=new AdminServiceImpl();

    protected void service (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String servletPath = request.getServletPath();
        if("/admin/login".equals(servletPath)){
            doLogin(request,response);
        }else if("/admin/back".equals(servletPath)){
            doBack(request,response);
        }else if("/admin/update".equals(servletPath)){
            doUpdate(request,response);
        }else if("/admin/select".equals(servletPath)){
            doSelect(request,response);
        }
    }

    private void doSelect(HttpServletRequest request,HttpServletResponse response) throws IOException{
        int id= Integer.parseInt(request.getParameter("id"));
        String username=adminService.getUserName(id);
        response.setContentType("text/text;charset=UTF-8");
        response.getWriter().write(username);
    }

    private void doLogin(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String account=request.getParameter("account");
        String password=request.getParameter("password");
        String flag=adminService.login(account,password);
        if(flag==null){
            flag="false";
        }else if(flag.equals("staff")){
            HttpSession session=request.getSession();
            session.setAttribute("account",account);
            session.setAttribute("pwd",password);
            request.getRequestDispatcher("/user.jsp").forward(request,response);
        }else if(flag.equals("admin")){
            response.sendRedirect(request.getContextPath()+"/admin.jsp");
        }else {
            flag="false";
        }
        response.setContentType("text/text;charset=UTF-8");
        response.getWriter().write(""+flag);
    }

    private void doBack(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("back",true);
        request.getRequestDispatcher("/login.jsp").forward(request,response);
    }

    private void doUpdate(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String pwd=request.getParameter("pwd");
        int staff_id= Integer.parseInt(request.getParameter("id"));
        HttpSession session=request.getSession();
        session.setAttribute("pwd",pwd);
        adminService.update(new Staff(staff_id,pwd));
        response.setContentType("text/text;charset=UTF-8");
        response.getWriter().write(""+"success");
    }
}
